﻿using System;
using System.Text;
using System.Data;
using System.IO;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class WeeklyScheduleItemCls
    {
        public string WeeklyScheduleItemId="";
        public string ScheduleDate = "";
        public string DayOfWeek = "";
        public string Time = "";
        public string ForeColor = "";
        public string BelongSite = "";
        public string frkWeeklyScheduleId = "";
        public string ScheduleText = "";
        public string Segment = "0";
    }

    public class WeeklyScheduleItemParser
    {
        public static WeeklyScheduleItemCls CreateInstance()
        {
            //tao 1 class trang....
            WeeklyScheduleItemCls oWeeklyScheduleItemCls = new WeeklyScheduleItemCls();
            return oWeeklyScheduleItemCls;
        }

        public static WeeklyScheduleItemCls ParseFromXml(string XmlData)
        {
            if (string.IsNullOrEmpty(XmlData)) return null;
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            WeeklyScheduleItemCls oWeeklyScheduleItemCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oWeeklyScheduleItemCls;
        }


        public static WeeklyScheduleItemCls ParseFromDataRow(DataRow dr)
        {
            WeeklyScheduleItemCls oWeeklyScheduleItemCls = new WeeklyScheduleItemCls();
            oWeeklyScheduleItemCls.WeeklyScheduleItemId = XmlUtility.GetString(dr, "WeeklyScheduleItemId", true);
            oWeeklyScheduleItemCls.ScheduleDate = XmlUtility.GetDate(dr, "ScheduleDate", true).ToString();
            oWeeklyScheduleItemCls.DayOfWeek = XmlUtility.GetString(dr, "DayOfWeek", true);
            oWeeklyScheduleItemCls.Time = XmlUtility.GetString(dr, "Time", true);
            oWeeklyScheduleItemCls.BelongSite = XmlUtility.GetString(dr, "BelongSite", true);
            oWeeklyScheduleItemCls.frkWeeklyScheduleId = XmlUtility.GetString(dr, "frkWeeklyScheduleId", true);
            oWeeklyScheduleItemCls.ScheduleText = XmlUtility.GetString(dr, "ScheduleText", true);
            oWeeklyScheduleItemCls.Segment = XmlUtility.GetString(dr, "Segment", true);

            return oWeeklyScheduleItemCls;
        }

        public static WeeklyScheduleItemCls[] ParseFromDataTable(DataTable dtTable)
        {
            WeeklyScheduleItemCls[] WeeklyScheduleItems = new WeeklyScheduleItemCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                WeeklyScheduleItems[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return WeeklyScheduleItems;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");
     
            ds.Tables["Info"].Columns.Add("WeeklyScheduleItemId");
            ds.Tables["Info"].Columns.Add("ScheduleDate");
            ds.Tables["Info"].Columns.Add("DayOfWeek");
            ds.Tables["Info"].Columns.Add("Time");
            ds.Tables["Info"].Columns.Add("ForeColor");
            ds.Tables["Info"].Columns.Add("BelongSite");
            ds.Tables["Info"].Columns.Add("frkWeeklyScheduleId");
            ds.Tables["Info"].Columns.Add("ScheduleText");
            ds.Tables["Info"].Columns.Add("Segment");
            return ds;
        }

        public static string GetXml(WeeklyScheduleItemCls oWeeklyScheduleItemCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oWeeklyScheduleItemCls.WeeklyScheduleItemId,
                oWeeklyScheduleItemCls.ScheduleDate,
                oWeeklyScheduleItemCls.DayOfWeek,
                oWeeklyScheduleItemCls.Time,
                oWeeklyScheduleItemCls.ForeColor,
                oWeeklyScheduleItemCls.BelongSite,
                oWeeklyScheduleItemCls.frkWeeklyScheduleId,
                oWeeklyScheduleItemCls.ScheduleText,
                oWeeklyScheduleItemCls.Segment,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            WeeklyScheduleItemCls oWeeklyScheduleItem = WeeklyScheduleItemParser.ParseFromDataRow(dr);
            string Xml=WeeklyScheduleItemParser.GetXml(oWeeklyScheduleItem);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            WeeklyScheduleItemCls[] WeeklyScheduleItems = new WeeklyScheduleItemCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                WeeklyScheduleItems[iIndex] = WeeklyScheduleItemParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = WeeklyScheduleItemParser.GetXml(WeeklyScheduleItems);
            return Xml;
        }


        public static string GetXml(WeeklyScheduleItemCls[] WeeklyScheduleItems)
        {
            if (WeeklyScheduleItems == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                WeeklyScheduleItems = new WeeklyScheduleItemCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < WeeklyScheduleItems.Length; iIndex++)
            {
                WeeklyScheduleItemCls oWeeklyScheduleItemCls = WeeklyScheduleItems[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oWeeklyScheduleItemCls.WeeklyScheduleItemId,
                    oWeeklyScheduleItemCls.ScheduleDate,
                    oWeeklyScheduleItemCls.DayOfWeek,
                    oWeeklyScheduleItemCls.Time,
                    oWeeklyScheduleItemCls.ForeColor,
                    oWeeklyScheduleItemCls.BelongSite,
                    oWeeklyScheduleItemCls.frkWeeklyScheduleId,
                    oWeeklyScheduleItemCls.ScheduleText,
                    oWeeklyScheduleItemCls.Segment,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static WeeklyScheduleItemCls[] ParseMultiRecordFromXml(string XmlData)
        {
            if (string.IsNullOrEmpty(XmlData))
            {
                return new WeeklyScheduleItemCls[0];
            }
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if(ds.Tables.Count==0)
            {
                return new WeeklyScheduleItemCls[0];
            }
            WeeklyScheduleItemCls[] WeeklyScheduleItems = new WeeklyScheduleItemCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                WeeklyScheduleItems[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return WeeklyScheduleItems;
        }
    }
}
